package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniqueProfession;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.SysDimProfession;
import cn.micro.system.manage.entity.dto.SysDimProfessionSearch;
import cn.micro.system.manage.service.SysDimProfessionService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 职务表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2023-12-29
 */
@RestController
@RequestMapping("/manage/sysDimProfession")
@RequiredArgsConstructor
@Validated
public class SysDimProfessionController {

    private final SysDimProfessionService sysDimProfessionService;

    @GetMapping("/{page}/{size}")
    @PreAuthorize("hasAuthority('bnt.profession.list')")
    public Object list(@PathVariable int page, @PathVariable int size, SysDimProfessionSearch search){
        return Result.success(new PageInfo<>(sysDimProfessionService.listByPage(page,size,search)));
    }

    @GetMapping("detail/{id}")
    @PreAuthorize("hasAuthority('bnt.profession.list')")
    public Object detail(@PathVariable Long id){
        return Result.success(sysDimProfessionService.getById(id));
    }

    @PostMapping("save")
    @Log(title = "职务管理--新增职务信息", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.profession.add')")
    public Object save(@Validated(value = ValidateGroup.add.class) @UniqueProfession @RequestBody SysDimProfession sysDimProfession){
        if (!sysDimProfessionService.save(sysDimProfession)){
            return Result.fail();
        }
        return Result.success();
    }

    @PutMapping("edit")
    @Log(title = "职务管理--修改职务信息", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.profession.update')")
    public Object edit(@Validated(value = ValidateGroup.edit.class) @UniqueProfession @RequestBody SysDimProfession sysDimProfession){
        if (!sysDimProfessionService.updateById(sysDimProfession)){
            return Result.fail();
        }
        return Result.success();
    }

    @DeleteMapping("remove/{id}")
    @Log(title = "职务管理--删除职务信息", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.profession.remove')")
    public Object remove(@PathVariable Long id){
        if (!sysDimProfessionService.removeById(id)){
            return Result.fail();
        }
        return Result.success();
    }

    //同一个库

    @GetMapping("dropList")
    public Object dropList(){
        return Result.success(sysDimProfessionService.list());
    }
}
